package com.bawei.movie

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.entity.MovieVideoEntity
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.movie.adapter.MoviePagingDataAdapter
import com.bawei.movie.databinding.FragmentMovieListBinding
import com.bawei.movie.vm.MovieViewModel
import com.bumptech.glide.Glide
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


@Route(path = RoutePath.PATH_FRAGMENT_MOVIE_LIST)
@AndroidEntryPoint
class MovieListFragment : BaseMviFragment<FragmentMovieListBinding>() {

    private val mViewModel: MovieViewModel by activityViewModels()

    @Autowired
    @JvmField
    var tabId: Int = -1

    private lateinit var movieListAdapter: MoviePagingDataAdapter

    override fun getLayoutId(): Int = R.layout.fragment_movie_list

    override fun initViews() {
        ARouter.getInstance().inject(this)

        movieListAdapter = MoviePagingDataAdapter()
        mDataBinding.rvMovie.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = movieListAdapter
        }

        movieListAdapter.setItemClickListener(object : MoviePagingDataAdapter.MovieItemClickListener{
            override fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity) {
               ARouter.getInstance().build(RoutePath.PATH_DETAIL_PAGE).withSerializable("movieEntity", entity).navigation()
            }
        })

        mDataBinding.refreshLayout.setOnRefreshListener {
            movieListAdapter.refresh()
        }

        //设置RecyclerView固定布局和缓存大小
        mDataBinding.rvMovie.apply {
            setHasFixedSize(true)
            isNestedScrollingEnabled = false
            setItemViewCacheSize(20)

            //关闭Item动画
            (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        }

        //设置RecyclerView的缓存池
        var cachePool = RecyclerView.RecycledViewPool().apply {
            setMaxRecycledViews(0, 5) //其中viewType必须和适配器中Item类型保持一致
        }
        mDataBinding.rvMovie.setRecycledViewPool(cachePool)

        mDataBinding.rvMovie.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE){
                    Glide.with(this@MovieListFragment).resumeRequests()
                }else{
                    Glide.with(this@MovieListFragment).pauseRequests()
                }
            }
        })

        //加载数据
        loadDataWithPaging3()
    }

    private fun loadDataWithPaging3(){
        lifecycleScope.launch {
            if (tabId == -1){
                mViewModel.loadRecommendVideo().collectLatest {
                    if (mDataBinding.refreshLayout.isRefreshing){
                        mDataBinding.refreshLayout.finishRefresh()
                    }
                    //提交数据到适配器
                    movieListAdapter.submitData(it)
                }
            }else{
                mViewModel.loadMutilVideoById(tabId).collectLatest {
                    if (mDataBinding.refreshLayout.isRefreshing){
                        mDataBinding.refreshLayout.finishRefresh()
                    }
                    //提交数据到适配器
                    movieListAdapter.submitData(it)
                }

            }
        }
    }




}